package com.training1.core.common.global.signIn;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.training1.core.common.entity.BOXUserEntity;
import com.training1.core.common.utils.StringUtils;
import com.training1.core.common.utils.http.IPUtil;


/**
 * 登陆类
 * @author hl 2015-12-23
 *
 */
public class BOXLoginInfoUtil {

	public static final String BOXLOGININFO = "LOGININFO";
	
	private static ThreadLocal<BOXLoginInfo> threadLocal = new ThreadLocal<BOXLoginInfo>();
	
	private static final Logger LOG = LoggerFactory.getLogger(BOXLoginInfoUtil.class);
	
	private static  void setLoginInfo(BOXLoginInfo info){
		threadLocal.set(info);
	}
	
	/**
	 * 返回loginInfo
	 * @return
	 */
	public static BOXLoginInfo getLoginInfo(){
		BOXLoginInfo info = threadLocal.get();
		if(info==null){
			info = new BOXSimpleLoginInfo();
		}
		return info;
	}
	
//	private static LoginInfo getLoginInfo(HttpServletRequest request){
//		LoginInfo info = (LoginInfo)request.getSession().getAttribute(LOGININFO);
//		if(info==null){
//			LOG.debug("用户未登陆");BOXUserEntity
//			return new SimpleLoginInfo();
//		}
//		return info;
//	}
	
	public static BOXLoginInfo removeLoginInfo(){
		BOXLoginInfo info = getLoginInfo();
		threadLocal.remove();
		return info;
	}
	
	private static BOXLoginInfo createLoginInfo(BOXUserEntity user,String ip){
		return new BOXSimpleLoginInfo(user,ip);
	}
	
	/**
	 * 把登陆信息记录到session里
	 * @param request
	 * @param username
	 * @param ip 若不填则为request里取
	 */
	public static void setLoginInfoToSession(HttpServletRequest request,BOXUserEntity user,String ip){
		if(StringUtils.isBlank(ip)){
			ip = IPUtil.getIp(request);
		}
		BOXLoginInfo info = createLoginInfo(user, ip);
		LOG.info("用户登陆，用户名为{},id为{},ip为{},登陆时间为{}",info.getLoginUser().getUserName(),info.getLoginUser().getId(),info.getLoginIp(),info.getLoginTime());
		HttpSession session = request.getSession();
		session.setAttribute(BOXLOGININFO, info);
		setLoginInfo(info);
	}
	
	/**
	 * 把session里的登陆信息放到线程中
	 * @param request
	 */
	public static BOXLoginInfo copyLoginToThread(HttpServletRequest request){
		Object info = request.getSession().getAttribute(BOXLOGININFO);
		if(info!=null&& info instanceof BOXLoginInfo){
			setLoginInfo((BOXLoginInfo)info);
		}
		
		return (BOXLoginInfo)info;
	}
	
	/**
	 * api端调用
	 * @param cellphone 手机号
	 */
	public static void setLoginCellphone(String cellphone){
		BOXUserEntity user = new BOXUserEntity();
		user.setPhone(cellphone);
		BOXLoginInfo info = new BOXSimpleLoginInfo(user,true);
		setLoginInfo(info);
	}
	/**
	 * 登出
	 * @param request
	 */
	public static void signOut(HttpServletRequest request){
		if(request!=null){
			HttpSession session = request.getSession();
			session.removeAttribute(BOXLOGININFO);
			session.invalidate();
		}
		
		signOut(IPUtil.getIp(request));
	}
	
	private static void signOut(String ip){
		BOXLoginInfo info = removeLoginInfo();
		LOG.info("用户主动登出，用户名为{},ip为{},登出时间为{}",info.getLoginName(),ip,new Date());
	}
	
	/**
	 * 判断是否登陆，并把登陆信息放到线程中
	 * @param request
	 * @return
	 */
	public static boolean isSignIn(){
		return getLoginInfo().isSignIn();		
	}
	
}
